
Refresh.Web.Localization = Class.create();
Refresh.Web.Localization = {

	_languages: new Object(),

	_defaultLanguage: 'en-us',
	
	_currentLanguage: null,

	addLanguage: function(language, values) {
		language = language.toLowerCase();
		
	
		this._languages[language] = values;
		this._currentLanguage = language;
	},
	
	hasLanguage: function(language) {
		language = language.toLowerCase();
		return (this._languages[language] != null);		
	},	

	getValue: function(key, language) {
		key = key.toLowerCase();		

		// check for language parameter
		if (typeof(language) == 'undefined') {
			language = this._currentLanguage;
		}
		
		language = language.toLowerCase();

		// check if it's been loaded
		if (!this.hasLanguage(language)) {
			
			language = this._defaultLanguage;		
		}
		
		var value = this._languages[language][key];

		if (typeof(value) == 'undefined' || value == null) {
			// try to load english one
			if (language != this._defaultLanguage)
				value = this._languages[this._defaultLanguage][key];

			// if still no luck, then
			if (typeof(value) == 'undefined' || value == null)
				value = 'MISSING:' + language + ':' + key;

		}

		return value;

	},

	updateLanguageElements: function(doc, language) {
		var elements = document.getElementsByClassName('langRec',doc);

		for (var i=0; i<elements.length; i++) {
			var key = elements[i].getAttribute('langKey');
			if (key == null) key = elements[i].id;
			
			if (elements[i].tagName.toLowerCase() == 'input') {
				elements[i].value = this.getValue(key, language);
			} else {			
				elements[i].innerHTML = this.getValue(key, language);
			}
		}
	}
};